package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDataDictionaryService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dataDictionary")
public class DataDictionaryController {
    @Autowired
    IDataDictionaryService dataDictionaryService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"dataDictionary:view","数据字典页面"},logical= Logical.OR)
    public String view() {
        return "dataDictionary";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult getAll(QueryObject dataDictionaryQuery) {
        return dataDictionaryService.query(dataDictionaryQuery);
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public List<DataDictionary> selectAll(QueryObject dataDictionaryQuery) {
        List<DataDictionary> dataDictionaries = dataDictionaryService.selectAll();
        return dataDictionaryService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"dataDictionary:save","保存数据字典"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(DataDictionary dataDictionary){
        try {
            dataDictionaryService.saveOrUpdate(dataDictionary);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("delete")
    @RequiresPermissions(value = {"dataDictionary:change","修改数据字典状态"},logical= Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        try {
            dataDictionaryService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"删除执行失败");
        }
        return new JSONResult(true,"删除执行成功");
    }

}
